#!/bin/bash

# Slackware build script for atari800

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20220615 bkw:
# - update for 5.0.0.
# - add man page for new cart utility.

# 20220407 bkw: BUILD=2, strip binary.

# 20200303 bkw:
# - update for 4.2.0, can still build 3.1.0, 4.0.0, 4.1.0.
# - move binary to /usr/games, man page to section 6.

# 20191126 bkw:
# - update for v4.1.0. script can still build VERSION=3.1.0 or 4.0.0.

# 20180703 bkw:
# - update for v4.0.0. script can still build VERSION=3.1.0 if needed.
# - update README to mention the new Altirra OS stuff.
# - minor slack-desc and README fix.
# - don't install INSTALL in /usr/doc.
# - minor script tweaks.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=atari800
VERSION=${VERSION:-5.0.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# allow building without OpenGL, in case someone needs it
if [ "${OPENGL:-yes}" = "yes" ]; then
  GLWITH="with"
else
  GLWITH="without"
fi

GLOPT="--$GLWITH-opengl"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz || \
  tar xvf $CWD/$PRGNAM-$VERSION-src.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
  \( -perm /111 -a \! -perm 755 \) -exec chmod 755 {} + -o \
  \! -perm /111 -exec chmod 644 {} +

DOCDIR=/usr/doc/$PRGNAM-$VERSION
PKGDOCDIR=$PKG/$DOCDIR

[ ! -f configure ] && cd src

# Include some optional extras. The monitor stuff is nice if you're
# developing code for the Atari, and the extra sound stuff makes for
# a more accurate emulation. The R: device options might let someone
# run an old-school Atari BBS you can telnet to.
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  $GLOPT \
  --with-video=sdl \
  --with-sound=sdl \
  --enable-monitorprofile \
  --enable-monitorbreakpoints \
  --enable-monitortrace \
  --enable-seriosound \
  --enable-riodevice \
  --enable-rnetwork \
  --enable-volonlysound \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --bindir=/usr/games \
  --mandir=/usr/man \
  --docdir=$DOCDIR \
  --build=$ARCH-slackware-linux

# in 4.0.0 and 4.1.0, the --enable-volonlysound is supposed to
# define this, but for some reason it doesn't.
#[ -e src/config.h ] && echo '#define VOL_ONLY_SOUND 1' >> src/config.h
# Unfortunately, forcibly defining it like that, doesn't make the volume
# only sound actually work. Must determine WTF's going on here.

make
make install-strip DOC_DIR=$DOCDIR MAN_DIR=/usr/man/man1 DESTDIR=$PKG

# 20200303 bkw: move man page to section 6 (games)
mkdir -p $PKG/usr/man/man6
sed '/^\.TH  *ATARI800  *1/s| 1 | 6 |' $PKG/usr/man/man1/$PRGNAM.1 | \
    gzip -9c > \
    $PKG/usr/man/man6/$PRGNAM.6.gz
rm -rf $PKG/usr/man/man1

gzip -9c < $CWD/cart.6 > $PKG/usr/man/man6/cart.6.gz

cat $CWD/$PRGNAM.SlackBuild > $PKGDOCDIR/$PRGNAM.SlackBuild
[ ! -d ../DOC ] && cd src

# 'make install' misses a lot of the docs, let's add them.
cd ../DOC
rm -f directx.txt $PKGDOCDIR/INSTALL
cp -a BUGS CREDITS ChangeLog FAQ TODO *.txt $PKGDOCDIR
cd ..

# util/ is worth keeping, but mostly for reference (so it goes in /usr/doc)
chmod -x util/*.* util/export
cp -a util $PKGDOCDIR

# the act/*.act files are palettes from the dark ages, leave them out.

# nice copyright-friendly icon
mkdir -p $PKG/usr/share/pixmaps
cat data/atari2.png > $PKG/usr/share/pixmaps/$PRGNAM.png

# .desktop file written for this build
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/install
sed "s,@WITH@,$GLWITH," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
